/**
 * 
 */
package com.android.hw.projectmenu;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;
import com.android.internal.telephony.PhoneConstants;
import com.android.internal.telephony.Phone;
import com.android.internal.telephony.PhoneFactory;
import android.telephony.SubscriptionManager;
import com.qualcomm.qcnvitems.QcNvItems;
/**
 * @author gaoshengpan
 *
 */
public class OthersInfo extends PreferenceActivity {
    
     private static final String KEY_IMEI_MEID = "imei_meid";
     private static final String KEY_ESN_NUM = "esn_num";
     private static final String KEY_SN_NUM = "sn_num";
     private static final String KEY_NV_STATUS = "nv_status";
     private static String mUnknown = null;
     private Phone mPhone = null;
     private QcNvItems mNv;
     private String snNUm = "";
     private String TAG = "OthersInfo";
     private Handler mHandler = new Handler() {

         @Override
         public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);
			switch (msg.what) {

			case 0:
				
				break;
			case 1:
				setSummaryText(KEY_SN_NUM, readSN());
				setSummaryText(KEY_IMEI_MEID, getImeiMeid());
				break;
			default:
				break;
			}
         }
      };

    /**
     * 
     */
    public OthersInfo() {
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.others_info);
        int numPhone = TelephonyManager.getDefault().getPhoneCount();
        Log.d(TAG, "phoneCount---------->" + numPhone);
        mPhone = PhoneFactory.getPhone(0);
        mNv = new QcNvItems(this);
        if (mPhone != null) {
            Log.d(TAG, "phone---------->notNull" + numPhone);
        } else {
            Log.d(TAG, "phoneType---------->null" + numPhone);
        }

    }

    /* (non-Javadoc)
     * @see android.app.Activity#onResume()
     */
    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        setPreferenceSummary();
    }

    public void setPreferenceSummary() {
        if (mPhone == null) {
            mPhone = PhoneFactory.getPhone(0);
        }

        if (mPhone != null) {
            Log.d(TAG, "set---------->" + KEY_ESN_NUM);
            getSN();
            setSummaryText(KEY_ESN_NUM, mPhone.getEsn());
//            setSummaryText(KEY_NV_STATUS, mUnknown);
        }

    }

    private void setSummaryText(String preferenceKey, String value) {
        Preference preference = findPreference(preferenceKey);
        if (preference == null)
            return;

        if (TextUtils.isEmpty(value))
            preference.setSummary(mUnknown);
        else
            preference.setSummary(value);
    }
    
    
    private String getImeiMeid() {
        TelephonyManager mtm = TelephonyManager.getDefault();
        String res = "";
        if (mNv == null) {
            mNv = new QcNvItems(this);
        }

        if (TelephonyManager.getDefault().isMultiSimEnabled()) {
            Log.d(TAG, "mPhone.getMeid()---------->" + mPhone.getMeid());
            Log.d(TAG, "mtm.getDeviceId(1)---------->" + mtm.getDeviceId(1));
            String meid = getString(R.string.meid_num) + mPhone.getMeid();
            String imei1 = "";
            String imei2 = getString(R.string.imei_num) + mtm.getDeviceId(1);
            try {
                imei1 = getString(R.string.imei_num) + mNv.getImei_1();
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
           
            res = getString(R.string.sim1) + "\n" + meid + "\n" + imei1 + "\n"
                    + getString(R.string.sim2) + "\n" + imei2;
        }

        return res;

    }
    
    public String getSN() {
        String info = null;
        if (mNv == null) {
            mNv = new QcNvItems(this);
        }
        if(mNv != null){
         mHandler.sendEmptyMessage(1);
         }

        if (info == null) {
            return mUnknown;
        }
        return info;
    }
 
    private String readSN() {
        String sn = null;
        try {
        	/*15-09-08 gaoshenpan fix just show hwSN begin*/
        	/*gaoshenpan 15-08-19 fix sn size begin*/
			sn = mNv.getSNNumber1().substring(0, 31);
			/*gaoshenpan 15-08-19 fix sn size end*/
			/*15-09-08 gaoshenpan fix just show hwSN end*/
            Log.d(TAG, "readSN -----:" + sn);
        } catch (Exception e) {
            // TODO: handle exception
            Log.e(TAG, "getSN e:" + e);
        }
        Log.e(TAG, "getSN info :" + sn);
        return sn;
    }
    
    /* (non-Javadoc)
     * @see android.preference.PreferenceActivity#onDestroy()
     */
    @Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if (mNv != null) {
			mNv.dispose();
		}
	}
    
}
